﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="UserController.cs" company="DissingNellemann">
//   
// All rights reserved </copyright>
// <summary>
//   The user controller.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
namespace Website.Controllers
{
    using System.Web.Mvc;

    using Website.CRUDServices;
    using Website.Models;
    using Website.Models.ViewModels;

    using User = Website.Models.Domain.User;

    /// <summary>
    ///     The user controller.
    /// </summary>
    public class UserController : BaseController
    {
        #region Public Methods and Operators

        /// <summary>
        ///     The edit self.
        /// </summary>
        /// <returns>
        ///     The <see cref="ActionResult" />.
        /// </returns>
        public ActionResult EditSelf()
        {
            var theUser = this.Session["LoggedInUser"] as User;
            return View(theUser);
        }

        /// <summary>
        /// The edit self.
        /// </summary>
        /// <param name="updatedUser">
        /// The updated user.
        /// </param>
        /// <returns>
        /// The <see cref="ActionResult"/>.
        /// </returns>
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult EditSelf(UserEdited updatedUser)
        {
            if (!this.ModelState.IsValid)
            {
                this.ViewData["ErrorMessage"] = "Der opstod en fejl. Prøv venligst igen.";
                return this.RedirectToAction("Index");
            }

            var theUser = this.Session["LoggedInUser"] as User;

            CRUDServiceProvider.WithCRUDService(
                client =>
                    {
                        var cmd = new UpdateUserCommand();
                        var dto = new CRUDServices.User
                                      {
                                          Name = theUser.Name = updatedUser.Name,
                                          Username = theUser.Username = updatedUser.Username,
                                          ID = theUser.ID = ((User)this.Session["LoggedInUser"]).ID,
                                          PhoneNumber = theUser.PhoneNumber = updatedUser.PhoneNumber,
                                          Email = theUser.Email = updatedUser.Email
                                      };
                        if (!string.IsNullOrWhiteSpace(updatedUser.Password)
                            && (updatedUser.Password == updatedUser.Password2))
                        {
                            var cmdU = new UpdateUserPasswordCommand
                                           {
                                               ID = ((User)this.Session["LoggedInUser"]).ID,
                                               NewPassword =
                                                   theUser.Password = updatedUser.Password
                                           };
                            client.ExecuteCommand(cmdU);
                        }

                        cmd.UpdatedUser = dto;
                        client.ExecuteCommand(cmd);
                        this.Session["LoggedInUser"] = theUser;
                    });
            return this.RedirectToAction("Index");
        }

        /// <summary>
        ///     The index.
        /// </summary>
        /// <returns>
        ///     The <see cref="ActionResult" />.
        /// </returns>
        public ActionResult Index()
        {
            this.Init("User");
            var theUser = this.Session["LoggedInUser"] as User;

            // pass the user for data transfer
            return View(theUser);
        }

        #endregion
    }
}